<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}


if($_REQUEST["sub"]=="save"){
    
   $id=trim($_POST["id"]);
   $thistoken=md5(EZKEY.$id.$_SESSION['EZ_Admin']['id']);
   if(empty($_POST["token"])){
      jumpajax('300','提交失败！',''); 
   }
   if($thistoken<>$_POST["token"]){
       jumpajax('300','提交失败！','');
            
   }
   $goodslist=array();
   $goods=$_POST["goods"];
   if(empty($goods)){
    jumpajax('300','没有提交需要打印标签的商品！','');
   }
   $glist=array();
   while(list($k,$v)=each($goods)){
     if(intval($v)>0){
        $glist[]=$k;
     }
   }
   if(empty($glist)){
    jumpajax('300','没有提交打印标签商品数量！','');
   }
   
   $thisprintlist=array();
   $prow=$ezhand->select_all("workman_checklist c,shopping3_goods g","g.*,c.ctime,c.checknum,c.printnum,c.checkman","c.goodsid=g.id and c.workmanid=".$id." and c.goodsid in (".implode(",",$glist).")");
   while(list($k,$v)=each($prow)){
    $gid=$v["id"];

    //if($v["checknum"]<$goods[$gid]){
    //     jumpajax('300',$v["checknum"].'提交打印标签数量大于质检标签数！'.$goods[$gid],'');       
    //}
    if(!isset($thisprintlist[$gid])){
        if($v["metlist"]<>""){
            $posstr=strpos($v["metlist"],",",3);
            $v["metlist"]=substr($v["metlist"],0,$posstr);
            
        }
        
    $thisprintlist["g".$gid]=array(
    "goodsid"=>$v["id"],
    "title"=>$v["title"],
    "goodssn"=>$v["goodssn"],
    "title_en"=>$v["title_en"],
    "outtime"=>date("Y-m-d H:i",$v["ctime"]),
    "weight"=>$v['weight']."克",
    "keepdays"=>$v["keepdays"]."天",
    "keepway"=>$v["keepway"],
    "metlist"=>$v["metlist"],
    "checkman"=>$v["checkman"],
    "qcode"=>$v["goodssn"]."-".$id."-",
    "printnum"=>$goods[$gid],
    "startprint"=>intval($v["printnum"])   
    );
    
    }else{
      $thisprintlist["g".$gid]["printnum"]=$thisprintlist["g".$gid]["printnum"]+$v["printnum"]; 
      $thisprintlist["g".$gid]["checknum"]=$thisprintlist["g".$gid]["checknum"]+$v["checknum"]; 
        
    }
    
    //$thisprintlist[]=$row;
   }
   $rt=array();
   while(list($k,$v)=each($thisprintlist)){
    $rt[]=$v;
   }
   
   
   jumpajax('220',json_encode($rt),'','');
    
    
}

$id=empty($_GET['id'])?0:(int)$_GET['id'];
if(empty($id)){

    error_html("没有找到加工单！","","");
}else{
    $workinfo=$ezhand->select("workman",'*','id="'.$id.'"');
    if(empty($workinfo["orderid"])){
        error_html("没有找到加工单！","","");
    }
    $codenumb=$workinfo["code_numb"];
    $ordernum=$workinfo["ordernum"];
    $work_items=$ezhand->select_all("workman_items",'*','wid="'.$id.'"');
    
    $goodsnum=$workinfo["subitems"];
    $metnum=$workinfo["metnums"];
    
    /*
    $where="o.id=og.orderid and o.id in (".$workinfo["orderid"].") and o.status in (1,2)  ";
    
    $ordergoods=$ezhand->select_all("shopping3_order o,shopping3_order_goods og",'og.goodsid,sum(og.total) as total',''.$where.'  group by og.goodsid ');

    $ordergoodstemp=$ordergoods;
    */
    $goodslisttemp=$ezhand->select_all("shopping3_goods","*","1");
    while(list($k,$v)=each($goodslisttemp)){
        $gid=$v["id"];
        $goodslist[$gid]=$v;    
        
    }
    
    
    /*
            //取得食材明细
        $materiallisttemp=$ezhand->select_all("material","*","1");
        while(list($k,$v)=each($materiallisttemp)){
            $pid1=$v["id"];
            $materiallist[$pid1]=$v;    
            
        }
        
        //取得菜品对应食材的明细
        $goodsmetlist=array();
        
        $metnamelist=array();
        $metlist=$ezhand->select_all("goods_items","*","1 group by tid,mid");
        while(list($k,$v)=each($metlist)){
         $tid=$v["tid"];
        
         $mid=$v["mid"];
        
         $goodsmetlist[$tid][]=$v;
         $metnamelist[$mid]=$v["mname"]; 
        }
        
        */
        
        /*
        $donelist=array();
        $donelisttemp=$ezhand->select_all("workman_done","*","workmanid=".$id);
        while(list($k,$v)=each($donelisttemp)){
            $gid=$v["goodsid"];
            $donelist[$gid]=$v;
        }        
        $blnum=$ezhand->count("workman","parentid=".$id);  
        $bllistrow= $ezhand->select_all("workman","*","parentid=".$id);
        */        
        //if($workinfo["zjstatus"]==2 or $workinfo["zjstatus"]==3){
        $checklist=$ezhand->select_all("workman_checklist","goodsid,title,goodssn,ordernum,sum(goodsnum) as goodsnum,sum(checknum) as checknum,sum(printnum) as printnum","workmanid=".$id." group by goodsid");
        //}
       // if(!empty($checklist)){
         //   $workinfo["zjstatus"]=3;
        //}
        
}

$token=md5(EZKEY.$id.$_SESSION['EZ_Admin']['id']);
?>